<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<script type="text/javascript">

	$(function(){
		$("#contentList").click(function(){
			location.href="${initParam.root}collection/contentList.do"; 
		});
		
		var str=$("#content").text();
		$("#letterCount").html(str.length); 
		$("#content").keyup(function(){
			var str="";
			str=$("#content").val();
			$("#letterCount").html(str.length); 
		});
		
		getComment(); //페이지 레디시 모든 댓글 호출
		$("#updateCollection").click(function(){ //게시글 수정 
			if(confirm("게시글을 수정하시겠습니까?")){
				location.href="${initParam.root}collection/auth_updateCollection.do?no=${requestScope.detailInfo.bvo.no}";
			}//if
		});//게시글을 삭제하는 구현부
		$("#deleteCollection").click(function(){ //게시글 삭제
			if(confirm("게시글을 삭제하시겠습니까?")){
				location.href="${initParam.root}collection/auth_deleteCollection.do?no=${requestScope.detailInfo.bvo.no}";
			}//if
		});//updateBoard click
		
		<%--댓글 관련 구현부 --%>
		$("#commentBtn").click(function(){ //댓글등록 버튼  
			if($("textarea[name=comment]").val()==""){
				alert("댓글의 내용이 없습니다.");
			}else{
			 $.ajax({
				type:"post",
				url:"${initParam.root}collection/auth_writeComment.do",
				data:$("#commentForm").serialize(),
				dataType:"json",
				success:function(flag){
					if(!flag){
						alert("댓글등록 실패");
					}
						$("textarea[name=comment]").val("");
						getComment();
				}//callback
			 });//ajax
			}
		});//commentBtn click
		
		$("#commentView").on("click",".updateComment",function(){
			var str = "<textarea rows='2' cols='40' class='commentText' style='resize: none;'>"+$(this).parent().parent().find(".textView").text()+"</textarea>";
			str+="<input style='vertical-align: 96%; height: 37px;' type='button' value='수정완료' class='updateCommentGo'>";
			$(this).parent().parent().find(".textView").html(str);
		});
		
		$("#commentView").on("click",".updateCommentGo",function(){
			//alert($(this).parent().find(".commentText").val());
			//alert($(this).parent().parent().find(".no").val());
			$.ajax({
				type:"post",
				url:"${initParam.root}collection/auth_updateComment.do",
				data:"no="+$(this).parent().parent().find(".no").val()+"&comment="+$(this).parent().find(".commentText").val(),
				dataType:"json",
				success : function(index,data){
					//alert("댓글업댓성공");
					getComment();
				}
			});
		});
		$("#replyWrite").click(function(){
			location.href="${initParam.root}collection/auth_replyWrite.do?no=${requestScope.detailInfo.bvo.no}&ref=${requestScope.detailInfo.bvo.ref}";  
		});
		
		$("#updateLike").click(function(){
			$.ajax({
				type:"post",
				url:"${initParam.root }collection/auth_updateLike.do?",
				data:"no=${requestScope.detailInfo.bvo.no }",
				dataType:"json",
				success:function(data){
					if(data){
						alert("추천되었습니다.");
						location.reload(true);
					}
					else{
						alert("이미 추천된 게시물입니다.");
					}
				}
			});
		});
	});//ready
	
	function getComment(){ //모든댓글을 가져오는 함수 정의
		$.ajax({
		type:"post",
		url:"${initParam.root}collection/auth_getAllComment.do",
		data:"no=${requestScope.detailInfo.bvo.no}", //게시물의 번호를 전송
		dataType:"json",
		success:function(data){
			var sessionId = "${sessionScope.loginMember.id}";
			var sessionPoint = "${sessionScope.loginMember.point}";
			var form="<tr><td colspan=3></td></tr>";
			$.each(data,function(index,value){
				form+="<form>";
				form+="<tr><td colspan=2>";
				form+="<b>"+value.memberVO.id+"</b>";
				form+="</td>";
				form+="<td align=right>";
				form+=value.date;
				form+="</td></tr>";
				form+="<tr><input type='hidden' class='no' value='"+value.no+"'>";
				form+="<td colspan=2 class='textView'><pre>";
				form+=value.comment;
				form+="</pre></td>";
				form+="<td align=right>";
				if(sessionId==value.memberVO.id){					
					form+="<a href='#' class='updateComment'>수정</a>";
					form+=" | ";
					form+="<a href='#' onclick='deleteComment("+value.no+")'>삭제</a>";
				}else if(sessionPoint==-1){
					form+="<a href='#' onclick='deleteComment("+value.no+")'>삭제</a>";
				}
				form+="</td>";
				form+="<tr style='border-bottom: 1px dashed silver;'><td colspan=3><br></td></tr>";
				form+="</form>";
			});
			$("#commentView").html(form); //댓글 영역에 표시한다
		}//callback
	});//ajax 
	}//getComment
	
	function updateCommentGo(no,comment){
		$.ajax({
				type:"get",
				url:"${initParam.root}collection/auth_updateComment.do",
				data:"no="+no+"&comment="+comment,
				//댓글정보를 전송
				dataType:"json",
				success:function(flag){
						getComment();
				}//callback
		});//ajax  
	}
	
	function deleteComment(no){
		//alert(no);
		if(confirm("댓글을 삭제 하시겠습니까?")){
			$.ajax({
				type:"post",
				url:"${initParam.root}collection/auth_deleteComment.do",
				data:"no="+no,
				dataType:"json",
				success: function(index,data){
					getComment();
				}
			});
		}	
	}
</script>
<table class="pub_detail_table" style="word-wrap: break-word; table-layout: fixed;">
	<thead>
		<tr id="thead">
			<td width="10%">${requestScope.detailInfo.bvo.no }</td>
			<td width="60%"><a href="${initParam.root}collection/contentList.do">자소서첨삭</a> | <b style="font-size: 11pt;">${requestScope.detailInfo.bvo.title}</b></td>
			<td align="right" width="30%">${requestScope.detailInfo.bvo.date}</td>
		</tr>
		<tr>
			<td colspan="2">작성자 | <b style="font-size: 10pt;">${requestScope.detailInfo.bvo.memberVO.id}</b></td>
			<td align="right">조회수 ${requestScope.detailInfo.bvo.hit} | 추천수 ${requestScope.detailInfo.bvo.like}</td>
		</tr>
	</thead>
	<tbody>
		<tr id="fileBar">
			<td colspan="2" style="border-right: 1px dotted silver">
				<pre id="content">${requestScope.detailInfo.bvo.content}</pre>
				<br>
				<span id="letterCount"></span>/3000자
			</td>
			<td valign="top" align="right">
				<c:choose>
					<c:when test='${fn:length(requestScope.detailInfo.fileList)!=0 }'>
						<c:forEach var="list" items="${requestScope.detailInfo.fileList }">
							<a href="${initParam.root }collection/auth_fileDownload.do?newfilename=${list.newFileName }&orgfilename=${list.orgFileName}">${list.orgFileName }</a><br>
						</c:forEach>
					</c:when>
					<c:otherwise>
						파일 없음
					</c:otherwise>
				</c:choose>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<c:if test="${sessionScope.loginMember.id!=requestScope.detailInfo.bvo.memberVO.id}">
					<a href="#" id="updateLike"><img height="15" src="${initParam.root }img/like.jpg"> 추천</a>
				</c:if>
			</td>
			<c:choose>
			<c:when test="${sessionScope.loginMember.id==requestScope.detailInfo.bvo.memberVO.id}">
				<td align="right"><a href="#" id="updateCollection">수정</a> | <a href="#" id="deleteCollection">삭제</a></td>
			</c:when>
			<c:when test="${sessionScope.loginMember.point==-1}">
				<td align="right"><a href="#" id="deleteBoard">삭제</a></td>
			</c:when>
			<c:otherwise>
				<td></td>
			</c:otherwise>
			</c:choose>
		</tr>
		<tr><td colspan=3></td></tr>
	</tbody>
	<tfoot id="commentView">
	</tfoot>
</table>
<form id="commentForm" method="post">
	<textarea rows="2" cols="71" name="comment" style="resize: none;"></textarea>
	<input type="hidden" name="memberVO.id" value="${sessionScope.loginMember.id }">
	<input type="hidden" name="boardNo" value="${requestScope.detailInfo.bvo.no}">
	<input style="vertical-align: 96%; height: 37px;" type="button" value="덧글입력" id="commentBtn">
</form>
<p align="right">
<input type="button" value="목록" id="contentList">
<input type="button" value="답글" id="replyWrite">
</p>

